<?php
namespace Admin\Controller;
use Think\Controller;
class MakeController extends Controller {
    protected function _initialize() {
        //===权限验证======
        if (!session('admin')) {
            $this->error('请先登录!', U('Index/index'));
        }
        $admin = session('admin');
        $this->assign('admin', $admin);
        //===系统信息======
        $system = D('System')->find(1);
        $this->assign('system', $system);
    }
    public function MakeA($identity='', $name=''){
          $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'make'=>'active',
            ),
            'sub'=>array(
                'name'=>'查询学生信息',
                'make_r'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'MakeR',
        );
        $this->assign('header', $header);
       /* if ($identity != '') {
            if ($name != '') {
                $student = D('Student')->where(['identity'=>$identity, 'name'=>$name])->find();
            } else {
                $student = D('Student')->where(['identity'=>$identity])->find();
            }
        }
        if ($name != '') {
            if ($identity != '') {
                $student = D('Student')->where(['identity'=>$identity, 'name'=>$name])->find();
            } else {
                $student = D('Student')->where(['name'=>$name])->find();
            }
        }*/
        $this->assign('student', $student);
        $this->display('make_a');

    }

    public function MakeB($name='',$identity='') {
        $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'make'=>'active',
            ),
            'sub'=>array(
                'name'=>'学生信息',
                'make_r'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'MakeR',
        );
        $this->assign('header', $header);
        if ($name != '') {
            $findMap['name'] = ['LIKE', '%'.$name.'%'];
        }
        if ($identity != '') {
            $findMap['identity'] = ['LIKE', '%'.$identity.'%'];
        }
        $model=D('Student')->where($findMap)->find();
        $this->assign('model',$model);
        $grade = M('Grade')->select();
        $this->assign('grade', $grade);
        
        $major = M('Major')->select();
        $this->assign('major', $major);
        $class = M('Class')->select();
        $this->assign('class', $class);
        $this->display('make_b');
       

    }  
    public function MakeR($identity='',$update_timeStart ='',$update_timeEnd ='',$water = '',$term_id = '',$student_id='') {
        if ($term_id == '') {
            //如果没选择学期，默认是系统设置的当前学期
            $system = D('System')->find(1);
            $term_id = $system['term_id'];
        }
        //===header信息处理======
        $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'make'=>'active',
            ),
            'sub'=>array(
                'name'=>'缴费记录列表',
                'make_r'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'MakeR',
        );
        $this->assign('header', $header);
        //查询信息
        $findFlag = 0;
        
        if (($identity!= '')&&($findFlag == 0)) {
            $student = M('Student')->where(['identity'=>$identity])->find();
            $findMap['student_id'] = ['eq', $student['id']];
             $findFlag = 1;
        }
         if (($student_id!= '')&&($findFlag == 0)) {
            $findMap['student_id'] = ['LIKE', '%'.$student_id.'%'];
        }

        if (($water!= '')&&($findFlag == 0)) {
            $findMap['water'] = ['LIKE', '%'.$water.'%'];
        }
        if($update_timeStart !='' && $update_timeEnd !=''){
            $findMap['update_time'] = ['between', [strtotime($update_timeStart), strtotime($update_timeEnd)]];
        }
       /* if (($term_id != '')&&($findFlag == 0)) {
            $findMap['term_id'] = ['LIKE', '%'.$term_id.'%'];
        }  */    //===分页获取信息======
        $model = M('Fee');
        $count = $model->where($findMap)->count();
        $page = new \Think\AdminPage($count,C('PAGE_NUM'));
        $show = $page->show();
        $list = $model->where($findMap)->order('id')->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $student = M('Student')->select();
        $this->assign('student', $student);
        
        $grade = M('Grade')->select();
        $this->assign('grade', $grade);
        if ($major_id != ''){
            $class = M('Class')->where(['major_id'=>$major_id])->select();
            $this->assign('class', $class);
        }else{
            $class = M('Class')->select();
            $this->assign('class', $class);
        }
       
        $term = M('Term')->select();
        $this->assign('term', $term);
        $major = M('Major')->select();
        $this->assign('major', $major);
        
        $this->display('make_r');
    }
    public function MakeC(){
          $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'make'=>'active',
            ),
            'sub'=>array(
                'name'=>'缴费项目',
                'make_C'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'MakeC',
        );
        $this->assign('header', $header);
        $id=I('get.id');
        //找到这个学生
        $student = D('Student')->find($id);
        $this->assign('student',$student);
        //找到该学生应交的学费项目
        $fee = D('Fee')->where(['student_id'=>$id])->select();
        $this->assign('fee',$fee);
        $model = D('Fee');
        if (IS_POST) {
            foreach ($_POST['over'] as $key=>$v) {
                $temp = $model->where(['student_id'=>$id,'term_id'=>$key])->find();
                if ($_POST['should'][$key]<$v+$temp['over']) {
                    $this->error('实交费用不能高出应交费用，请检查!');
                }
            }
            foreach ($_POST['over'] as $key=>$v) {
                unset($data);
                $temp = $model->where(['student_id'=>$id,'term_id'=>$key])->find();
                $data['over'] = $v+$temp['over'];
                $data['water'] = time().rand(1000,9999);
                $data['update_time'] = time();
                $data['name'] =I('post.name');
                $model->where(['student_id'=>$id,'term_id'=>$key])->data($data)->save();

            }
            $this->success('交费成功!', U('Make/MakeD',['student_id'=>$id]));
            
        }
        $this->display('make_c');
    }
    public function MakeD(){
        $header = array(
        'sup'=>array(
            'name'=>'缴费记录管理',
            'make'=>'active',
        ),
        'sub'=>array(
            'name'=>'缴费项目',
            'make_C'=>'active',
        ),
        'icon'=>'icon-usd',
        'url'=>'MakeC',
        );
        $this->assign('header', $header);
        //找到这个学的id
        $student_id=I('get.student_id');
        //找到这个学生
        $student=D('Student')->find($student_id);
        $this->assign('student',$student);
        //找到这个学生交费项
        $fee=D('Fee')->where(['student_id'=>$student_id])->select();
        $this->assign('fee',$fee);
        $this->display('make_d');
    }
    public function MakeE($student_id = 0, $termidStr = ''){
            $termidStr = trim($termidStr, ',');
            $termArr = explode(',', $termidStr);
            if ($termArr[0] == '') {
                $this->error('至少选择一个交费项进行打印!');
            }
            //需要打印的item
            $feeArr = [];
            foreach ($termArr as $term_id) {
                $feeArr[] = D('Fee')->where(['student_id'=>$student_id, 'term_id'=>$term_id])->find();
            }
            $this->assign('feeArr', $feeArr);
            $student = D('Student')->find($student_id);
            $this->assign('student',$student);
            $this->display('print');
    }
    public function MakeF(){
          $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'make'=>'active',
            ),
            'sub'=>array(
                'name'=>'查询缴费信息',
                'make_r'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'MakeR',
        );
        $this->assign('header', $header);
        $this->display('make_f');

    }
    public function MakeG($name='',$identity=''){
        $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'make'=>'active',
            ),
            'sub'=>array(
                'name'=>'查询缴费信息',
                'make_r'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'MakeR',
        );
        $this->assign('header', $header);
        if ($name != '') {
            $findMap['name'] = ['LIKE', '%'.$name.'%'];
        }
        if ($identity != '') {
            $findMap['identity'] = ['LIKE', '%'.$identity.'%'];
        }
        //找到这个学生
        $model=D('Student')->where($findMap)->find();
        $this->assign('model',$model);
        //找到这个学生的缴费情况
        $fee= D('fee')->where(['student_id'=>$model['id']])->select();
        $this->assign('fee',$fee);
      /*  $overSum=D('fee')->field('sum(over) as sum')->where(['student_id'=>$model['id']])->select();
        $overSum = $overSum[0]['sum'];
        $this->assign('overSum', $overSum);*/
        $this->display('make_g');
    }
    public function MakeU($id = 0) {
        $model = D('Fee');
        if (IS_POST) {
            $_POST['id'] = $id;
            if ($_POST['over'] > $_POST['should']) {
                $this->error('实交学费不能高出应交学费，请检查!');
            }
            if (!$model->create()) {
                $this->error($model->getError());
                return false;
            }
            unset($data);
            $data['water'] = date('Y').rand(1000,9999);
            $data['update_time'] = time();
            $data['over'] =I('post.over');
            $data['name'] =I('post.name');
            $model->where(['id'=>$id])->data($data)->save();
            $this->success('修改成功!', U('Make/MakeC',['id'=>$id]));
        }
        $Fee = $model->getById($id);
        if (!$Fee) {
            $this->error('非法操作!');
            return false;
        }
        $this->assign('model', $Fee);
        //===header信息处理======
        $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'Make'=>'active',
            ),
            'sub'=>array(
                'name'=>'修改缴费记录',
                'make_u'=>'active',
            ),
            'icon'=>'icon-usd',
            'url'=>'MakeR',
        );
        $this->assign('header', $header);
        
        $student = M('Student')->select();
        $this->assign('student', $student);
        
        $grade = M('Grade')->select();
        $this->assign('grade', $grade);
        
        $class = M('Class')->select();
        $this->assign('class', $class);
        
        $term = M('Term')->select();
        $this->assign('term', $term);
        $major = M('Major')->select();
        $this->assign('major', $major);
        
        $this->display('make');
    }
    public function delete() {
        if (IS_POST) {
            $id = I('post.id');
            $model = D('Fee');
            $model->delete($id);
            $data['info'] = '删除成功!';
            $data['status'] = 1;
            $this->ajaxReturn($data, 'JSON');
        }
    }

    public function MakeI() {
        if (IS_POST) {
            if (isset($_FILES["import"]) && ($_FILES["import"]["error"] == 0)) {
                $result = \importExecl($_FILES["import"]["tmp_name"]);
                if ($result["error"] == 1) {
                    $xlsData = $result["data"][0]["Content"];
                    foreach ($xlsData as $k => $v) {
                        if ($k <= 1) {
                            //跳过表头
                            continue;
                        }
                        
                        $data = [
                            "id"=>$v["0"],
                            "student_id"=>$v["1"],
                            "term_id"=>$v["2"],
                            "should"=>$v["3"],
                            "over"=>$v["4"],
                            "name"=>$v["5"],
                            "water"=>$v["6"],
                            "create_time"=>$v["7"],
                            "update_time"=>$v["8"],
                        ];
                        M('Fee')->data($data)->add();
                    }
                }
            }
            $this->success('导入成功!', U('Make/MakeR'));
        }
        //===header信息处理======
        $header = array(
            'sup'=>array(
                'name'=>'缴费记录管理',
                'make'=>'active',
            ),
            'sub'=>array(
                'name'=>'导入缴费记录',
                'make_i'=>'active',
            ),
            'icon'=>'icon-bell',
            'url'=>'MakeR',
        );
        $this->assign('header', $header);
        $this->display('Make_i');
    }
    public function MakeO($identity='',$update_timeStart ='',$update_timeEnd ='',$water = '') {
        //查询信息
        $findFlag = 0;
        
        if (($identity!= '')&&($findFlag == 0)) {
            $student = M('Student')->where(['identity'=>$identity])->find();
            $findMap['student_id'] = ['eq', $student['id']];
             $findFlag = 1;
        }
        if (($water!= '')&&($findFlag == 0)) {
            $findMap['water'] = ['LIKE', '%'.$water.'%'];
        }
        if($update_timeStart !='' && $update_timeEnd !=''){
            $findMap['update_time'] = ['between', [strtotime($update_timeStart), strtotime($update_timeEnd)]];
        } 
        $model = M('Fee');
        $list = $model->where($findMap)->order('id')->limit($page->firstRow.','.$page->listRows)->select();
        $xlsName = "缴费记录";
        
            $xlsCell = [
                ["id","ID"],
                ["student_id","身份证号"], 
                ["term_id","学期及交费项"],
                ["should","应缴金额"],
                ["over","实缴金额"],
                ["name","缴款经办人"],
                ["water","流水号"],
                ["create_time","创建时间"],
                ["update_time","更新时间"],
            ];
        $model = M('Fee');
        $xlsData = $model->where($findMap)->field('id,student_id,term_id,should,over,name,water,create_time,update_time')->order('id')->select();
        \exportExcel($xlsName, $xlsCell, $xlsData);
    }



}
